package com.keta.foundation.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.keta.foundation.dao.PermissionDAO;
import com.keta.foundation.entity.Permission;
import com.keta.foundation.exception.ExistedException;
import com.keta.foundation.service.PermissionService;
import com.keta.foundation.service.RolePermissionService;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	private PermissionDAO permissionDAO;
	
	@Autowired
	private RolePermissionService rolePermissionService;
	
	
	@Override
	public void delete(Permission permission) {
		rolePermissionService.delete(permission.getRolePermissions()); 
		permissionDAO.delete(permission);
	}

	@Override
	public void delete(List<Permission> permissions) {
		for(Permission permission : permissions){
			this.delete(permission);
		}
	}

	@Override
	public void save(Permission permission) {
		if(permission.getId()==null){
			if (permissionDAO.getBySnAndModuleId(permission.getSn(),permission.getModule().getId()) != null) {
				throw new ExistedException("已经存在" + permission.getSn());
			}
		}
		permissionDAO.save(permission);
	}

	@Override
	public void save(List<Permission> permissions) {
		for (Permission permission : permissions) {
			this.save(permission);
		}
	}
}
